//
//  MHLoadingHUD.m
//  MH
//
//  Created by SY on 2018/10/7.
//  Copyright © 2018年 SY. All rights reserved.
//

#import "MHPopupAlert.h"
static UIView *containerView;
static MHPopupAlert *singleObject = nil;

@interface MHPopupAlert ()<MBProgressHUDDelegate>

@property (strong, nonatomic) UIView *showView;
@property (strong, nonatomic) MBProgressHUD *hud;

@end

@implementation MHPopupAlert
- (instancetype)init
{
    
    @synchronized(self) {
        if (singleObject == nil) {
            singleObject = [super init];
        }
    }
    return singleObject;
}

+ (id) allocWithZone:(NSZone *)zone
{
    @synchronized (self) {
        if (singleObject == nil) {
            singleObject = [super allocWithZone:zone];
        }
    }
    return singleObject;
}

// 禁止copy
- (id) copyWithZone:(NSZone *)zone
{
    return self;
}

+ (instancetype)shareHUD
{
    @synchronized(self) {
        if(!singleObject) {
            singleObject = [[super allocWithZone:NULL] init];
            //            [singleObject initialization];
        }
    };
    return singleObject;
}

+ (void)showMessageAlertWithTitle:(NSString *)title message:(NSString *)message {
    [self showMessageAlertWithTitle:title message:message actionTitle:@"确定" actionHandler:nil];
}
+ (void)showMessageAlertWithTitle:(NSString *)title message:(NSString *)message actionTitle:(NSString *)actionTitle {
    [self showMessageAlertWithTitle:title message:message actionTitle:actionTitle actionHandler:nil];
}

+ (void)showMessageAlertWithTitle:(NSString *)title message:(NSString *)message actionTitle:(NSString *)actionTitle actionHandler:(void (^ __nullable)(void))actionHandler {
    //IOS8.0及以后，采用UIAlertController
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action;
        if (actionHandler) {
            action = [UIAlertAction actionWithTitle:actionTitle
                                              style:UIAlertActionStyleDefault
                                            handler:^(UIAlertAction *action){
                                                actionHandler();
                                            }];
        }else {
            action = [UIAlertAction actionWithTitle:actionTitle
                                              style:UIAlertActionStyleCancel
                                            handler:nil];
        }
        
        [alertController addAction:action];
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
        
    }
    
}

+ (void)showConfirmAlertWithTitle:(NSString *)title message:(NSString *)message yesTitle:(NSString *)yesTitle yesAction:(void (^ __nullable)(void))yesAction {
    [self showConfirmAlertWithTitle:title message:message yesTitle:yesTitle yesAction:yesAction cancelTitle:@"取消" cancelAction:nil];
}

+ (void)showConfirmAlertWithTitle:(NSString *)title message:(NSString *)message yesTitle:(NSString *)yesTitle yesAction:(void (^ __nullable)(void))yesAction cancelTitle:(NSString *)cancelTitle cancelAction:(void (^ __nullable)(void))cancelAction {
    [self showConfirmAlertWithTitle:title message:message firstActionTitle:yesTitle firstAction:yesAction secondActionTitle:cancelTitle secondAction:cancelAction];
}

+ (void)showConfirmAlertWithTitle:(NSString *)title message:(NSString *)message firstActionTitle:(NSString *)firstActionTitle firstAction:(void (^ __nullable)(void))firstAction secondActionTitle:(NSString *)secondActionTitle secondAction:(void (^ __nullable)(void))secondAction {
    //IOS8.0及以后，采用UIAlertController
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:firstActionTitle
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction *action){
                                                           if (firstAction)
                                                               firstAction();
                                                       }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:secondActionTitle
                                                          style:UIAlertActionStyleCancel
                                                        handler:^(UIAlertAction *action) {
                                                            if (secondAction)
                                                                secondAction();
                                                        }];
        
        [alertController addAction:action];
        [alertController addAction:action2];
        [[UIViewController getCurrentViewController] presentViewController:alertController animated:YES completion:nil];

    }
}
@end
